package com.briup.day31.guitu;

/**
 * 龟兔赛跑（版本一：理想竞赛）
 * */

public class TortoiseAndRabbitRace1 {
	
	static class Animal extends Thread{  
		long score; 
		@Override
		public void run() {
			int sum = 0;
			long t1 = System.currentTimeMillis();
			while (sum < 100) {
				int meter = (int)(Math.random() * 10 + 1);
				sum += meter;
				System.out.println(getName()+"本次跑了"+meter
						+"米,当前一共跑了"+ sum +"米。");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			long t2 = System.currentTimeMillis();
			System.out.println(getName()+"耗时"+(t2 - t1)+"毫秒。");
			score = t2 - t1;

		}
		
	}
	
	public static void main(String[] args) {
		
		Animal a1 = new Animal();
		Animal a2 = new Animal();
		
		a1.setName("乌龟🐢");
		a2.setName("兔兔🐇");
		
		a1.start();
		a2.start();
		
		/*try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(a1.score < a2.score ? a1.getName()+"获胜"
				: a1.score > a2.score ? a2.getName() + "获胜" : "平局");
		*/
		
		/*while (a1.score == 0 || a2.score == 0) {
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println("🐢的成绩:"+a1.score);
		System.out.println("🐇的成绩:"+a2.score);
		*/
		
		//线程通信技术,main排到a1和a2后运行*/
		try {
			a1.join();  //main排在a1之后
			a2.join();  //main排在a2之后
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(a1.score < a2.score ? a1.getName()+"获胜!"
				: a1.score > a2.score ? a2.getName() + "获胜!" : "平局!");
		
	}

}
